package com.hxit.sportem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxit.sportem.dao.EqmentDao;
import com.hxit.sportem.entity.Equipment;
import com.hxit.sportem.service.EqmentService;
import com.hxit.sportem.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EqmentServiceImpl implements EqmentService {

    @Autowired
    private EqmentDao eqmentDao;

    @Override
    public ResultVo showList(String name, int pageNum) {
        PageHelper.startPage(pageNum,5);
        List<Equipment> list = eqmentDao.showList(name);
        PageInfo<Equipment> pageInfo=new PageInfo<Equipment>(list);
        if(pageInfo.getList().size()>0) {
            return new ResultVo(200, "查询成功", pageInfo);
        }else{
            return new ResultVo(201,"查询失败",null);
        }
    }

    @Override
    public ResultVo deleteEqment(Integer id) {
        Equipment selectEqment = eqmentDao.getEqmentById(id);
        if(selectEqment == null){
            return new ResultVo(201,"删除失败",null);
        }else{
            eqmentDao.deleEqmentById(id);
            return new ResultVo(200,"删除成功",null);
        }

    }

    @Override
    public ResultVo updateEqment(Equipment equipment) {
        System.out.println(equipment);
        Equipment selectEqment = eqmentDao.getEqmentByName(equipment.getName());
        if(selectEqment == null){
            return new ResultVo(201,"更新失败",null);
        }else{
            eqmentDao.updateEqment(equipment);
            System.out.println(equipment.getName());
            return new ResultVo(200,"更新成功",null);
        }
    }

    @Override
    public ResultVo addEqment(Equipment equipment) {
        Equipment selectEqment = eqmentDao.getEqmentByName(equipment.getName());
        if (selectEqment == null){
            eqmentDao.addEqment(equipment);
            return new ResultVo(200,"添加成功",null);
        }else {
            return new ResultVo(201,"添加失败",null);
        }
    }

    @Override
    public ResultVo deleteBatch(Integer[] idList) {
        eqmentDao.deleteBatch(idList);
        return new ResultVo(200,"删除成功",null);
    }
}
